import {Component} from '@angular/core';
import { Storage } from '@ionic/storage';

import {
  ModalController,
  ViewController
} from 'ionic-angular';

import { Common } from '../../../../providers/common';

import _ from 'lodash';
import {ContactChoosePage} from "../../../contact/choose/choose.component";

@Component({
  selector: 'page-marketing-weekly-query',
  templateUrl: './query.html'
})
export class MarketingWeeklyQueryPage {
	moa_page_name: any = "page-marketing-weekly-query";
  moa_page_explain: any = "工作周报 - 高级查询";

  condition = null;

  defaultEmpByTree = [];

  //可以选择的最大日期
  maxDate = "";
  //结束日期可以选择的最小日期
  minEndDate = "1970-01-01";

  constructor(
              public modalCtrl: ModalController,
              public viewCtrl: ViewController,
              public storage: Storage,
              public common: Common) {
      //可以选择的日期不能超过今天

      this.maxDate =new Date().getFullYear()+"-12-31";
      this.initCondition();
      this.storage.get("advancedQueryCondition")
        .then(data => {
            console.log("test" + data);
            if (data != null) {
                this.condition = data;
            }
        });
      console.log(this.condition);
  }

  cancel() {
    this.viewCtrl.dismiss("close");
  }

  /**
   * [chooseEmp 选择人员]
   * @param {[type]} item [description]
   */
  chooseEmp() {
    console.log("##authorization.detail.chooseEmpnames 选择外出人员");
    let that = this;
    let modal = this.modalCtrl.create(ContactChoosePage, {
      selected: this.defaultEmpByTree,
      isSelectUser: true,
      isSelectOne: false
    });

    modal.present();
    modal.onDidDismiss((data: any[]) => {
      console.info(data);
      if (data) {
        that.defaultEmpByTree = data;
        this.condition.emp = _.toString(_.map(data, "name"));
      }
    });
  }

  ionChange(){
      //设置结束日期的最小可选日期等于起始日期
      this.minEndDate = this.condition.startdate;
  }

  /**
   * 保存搜索条件并关闭Modal返回List页面加载搜索结果
   */
  search() {
      /*if (this.condition.startdate == null || this.condition.startdate == "") {
        this.condition.startdate = (this.common.getDate(-30));
      }
      if (this.condition.enddate == null || this.condition.enddate == "") {
        this.condition.enddate = (this.common.getDate(-30));
      }*/
      this.storage.set("advancedQueryCondition", this.condition);
      this.viewCtrl.dismiss("true");
  }

  /**
   * 清空查询条件
   */
  clear() {
      this.initCondition();
      this.storage.remove("advancedQueryCondition");
  }

  /**
   * 初始化查询条件
   */
  initCondition() {
    this.condition = {
      "emp" : "",
      "startdate" : (this.common.getDate(-30)),
      "enddate" : (this.common.getNowFormatDate())
    }
  }

}


